原创作品 转载请注明出处http://blog.csdn.net/always2015/article/details/45536147
杨辉三角最显著的特点是每个数等于它上方两数之和,这是最容易想到的方法。我的AC代码如下:
#include <iostream>
using namespace std;
int main()
{
int n,a[30][30]= {0};
while(cin>>n)
{
for(int j=0; j<n; j++)
a[j][0]=1;//第一列全置为1
for(int j=1; j<n; j++)
{
for(int h=1; h<=j; h++)
{
a[j][h]=a[j-1][h-1]+a[j-1][h];//每个数是上面两数之和
}
}
//输出杨辉三角
for(int j=0; j<n; j++)
{
for(int h=0; h<=j; h++)
{
if(h!=j)
cout<<a[j][h]<<" ";
else
cout<<a[j][h];//最后一个数没有空格
}
cout<<endl;
}
cout<<endl;
}
return 0;
}